home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / srcuc.zip / DOSSYS.C < prev    next >
C/C++ Source or Header  |  1992-05-05  |  9KB  |  369 lines

  1. /* -*-C-*-
  2.  
  3. $Header: /scheme/dos386/microcode/RCS/dossys.c,v 1.1 1992/05/05 06:55:13 jinx Exp $
  4.  
  5. Copyright (c) 1992 Massachusetts Institute of Technology
  6.  
  7. This material was developed by the Scheme project at the Massachusetts
  8. Institute of Technology, Department of Electrical Engineering and
  9. Computer Science.  Permission to copy this software, to redistribute
  10. it, and to use it for any purpose is granted, subject to the following
  11. restrictions and understandings.
  12.  
  13. 1. Any copy made of this software must include this copyright notice
  14. in full.
  15.  
  16. 2. Users of this software agree to make their best efforts (a) to
  17. return to the MIT Scheme project any improvements or extensions that
  18. they make, so that these may be included in future releases; and (b)
  19. to inform MIT of noteworthy uses of this software.
  20.  
  21. 3. All materials developed as a consequence of the use of this
  22. software shall duly acknowledge such use, in accordance with the usual
  23. standards of acknowledging credit in academic research.
  24.  
  25. 4. MIT has made no warrantee or representation that the operation of
  26. this software will be error-free, and MIT is under no obligation to
  27. provide any services, by way of maintenance, update, or otherwise.
  28.  
  29. 5. In conjunction with products arising from the use of this material,
  30. there shall be no use of the name of the Massachusetts Institute of
  31. Technology nor of any adaptation thereof in any advertising,
  32. promotional, or sales literature without prior written consent from
  33. MIT in each case. */
  34.  
  35. #include <dos.h>
  36. #include <stdio.h>
  37. #include "dossys.h"
  38.  
  39. int dos_keyboard_input_available_p(void)
  40. {
  41.   union REGS inregs, outregs;
  42.   
  43.   inregs.h.ah = 0x0B;
  44.   intdos(&inregs, &outregs);
  45.   
  46.   return (outregs.h.al != 0);
  47. }
  48.  
  49. unsigned char dos_get_keyboard_character(void)
  50. {
  51.   union REGS inregs, outregs;
  52.   
  53.   inregs.h.ah = 0x07;
  54.   intdos(&inregs, &outregs);
  55.   
  56.   return (unsigned char) (outregs.h.al);
  57. }
  58.  
  59. int dos_poll_keyboard_character(unsigned char *result)
  60. {
  61.   union REGS inregs, outregs;
  62.   
  63.   inregs.h.ah = 0x06;
  64.   inregs.h.dl = 0xFF;
  65.   intdos(&inregs, &outregs);
  66.   
  67.   *result = (unsigned char) (outregs.h.al);
  68.   return ((outregs.x.flags & 0x40) == 0);
  69. }
  70.  
  71. void dos_console_write_character(unsigned char character)
  72. {
  73.   union REGS inregs, outregs;
  74.   
  75.   inregs.h.ah = 0x06;
  76.   inregs.h.dl = character;
  77.   
  78.   intdos(&inregs, &outregs);
  79.   return;
  80. }
  81.  
  82. int dos_console_write(void * vbuffer, size_t nsize)
  83. { union REGS inregs, outregs;
  84.   unsigned char *buffer = vbuffer;
  85.   int i;  
  86.   
  87.   for (inregs.h.ah = 0x06, i=0; i < nsize; i++)
  88.   { inregs.h.dl = buffer[i];
  89.     intdos(&inregs, &outregs);
  90.   }
  91.   return nsize;
  92. }
  93.  
  94.  
  95. /* DOS I/O functions using handles */
  96.  
  97. handle_t dos_open_file_with_handle(unsigned char * name, int mode)
  98. {
  99.   union REGS inregs, outregs;
  100.   struct SREGS segregs;
  101.   
  102.   inregs.e.edx = (unsigned long) name;
  103.   segread(&segregs);
  104.  
  105.   inregs.h.ah = 0x3D;
  106.   inregs.h.al = mode;
  107.   intdosx(&inregs, &outregs, &segregs);
  108.   printf("Returning from DOS\n");
  109.   return (outregs.x.cflag) ? DOS_FAILURE : (unsigned int) outregs.x.ax;
  110. }
  111.  
  112. int dos_close_file_with_handle(handle_t handle)
  113. {
  114.   union REGS inregs, outregs;
  115.   
  116.   inregs.x.bx = handle;
  117.   inregs.h.al = 0x3E;
  118.   intdos(&inregs, &outregs);
  119.   
  120.   return (outregs.x.cflag) ? DOS_FAILURE : DOS_SUCCESS;
  121. }
  122.  
  123. int dos_read_file_with_handle(handle_t handle, void * buffer, size_t nbytes)
  124. {
  125.   union REGS inregs, outregs;
  126.   struct SREGS segregs;
  127.   
  128.   inregs.x.bx = handle;  
  129.   inregs.e.edx = (unsigned long) buffer;
  130.   inregs.e.ecx = nbytes;
  131.  
  132.   segread(&segregs);
  133.  
  134.   inregs.h.ah = 0x3F;
  135.   intdosx(&inregs, &outregs, &segregs);
  136.   
  137.   return (outregs.x.cflag) ? DOS_FAILURE : outregs.e.eax;
  138. }
  139.  
  140. int dos_write_file_with_handle(handle_t handle, void * buffer, size_t nbytes)
  141. {
  142.   union REGS inregs, outregs;
  143.   struct SREGS segregs;
  144.   
  145.   inregs.x.bx = handle;
  146.   inregs.e.edx = (unsigned long) buffer;
  147.   inregs.e.ecx = nbytes;
  148.   
  149.   segread(&segregs);
  150.  
  151.   inregs.h.ah = 0x40;
  152.   intdosx(&inregs, &outregs, &segregs);
  153.   
  154.   return (outregs.x.cflag) ? DOS_FAILURE : outregs.e.eax;
  155. }
  156.   
  157. int dos_get_device_status_with_handle(handle_t handle)
  158.   union REGS inregs, outregs;
  159.   
  160.   inregs.x.bx = handle;
  161.   inregs.x.ax = 0x4400;
  162.   intdos(&inregs, &outregs);
  163.   
  164.   return (outregs.x.cflag) ? DOS_FAILURE : (unsigned int) outregs.x.dx;
  165. }
  166.   
  167. int dos_set_device_status_with_handle(handle_t handle, int mode)
  168. { int original_mode;
  169.   union REGS inregs, outregs;
  170.   
  171.   original_mode = dos_get_device_status_with_handle(handle);
  172.   if (original_mode == DOS_FAILURE) return DOS_FAILURE;
  173.  
  174.   inregs.x.dx = mode;
  175.   inregs.x.bx = handle;
  176.   inregs.x.ax = 0x4401;
  177.   intdos(&inregs, &outregs);
  178.   return (outregs.x.cflag) ? DOS_FAILURE : original_mode;
  179. }  
  180.  
  181.  
  182.  
  183. void dos_get_version(version_t *version_number)
  184. {
  185.   union REGS inregs, outregs;
  186.  
  187.   /* Use old style version number because we may be running below DOS 5.0 */
  188.   inregs.h.al = 0x01;
  189.   inregs.h.ah = 0x30;
  190.   intdos(&inregs, &outregs);
  191.   version_number -> major = outregs.h.al;
  192.   version_number -> minor = outregs.h.ah;
  193.  
  194.   if ((version_number -> major) >= 5)
  195.   { /* Get the real version. */
  196.     inregs.x.ax = 0x3306;
  197.     intdos(&inregs, &outregs);
  198.     version_number -> major = outregs.h.bl;
  199.     version_number -> minor = outregs.h.bh;
  200.   }
  201.   return;
  202. }
  203.  
  204. void dos_reset_drive(void)
  205. {
  206.   union REGS inregs, outregs;
  207.   
  208.   inregs.h.al = 0x0d;
  209.   intdos(&inregs, &outregs);
  210.   
  211.   return;
  212. }
  213.  
  214. int dos_set_verify_flag(int verify_p)
  215. { union REGS inregs, outregs;
  216.   int old_flag;
  217.   
  218.   inregs.h.ah = 0x54;
  219.   intdos(&inregs, &outregs);
  220.   old_flag = outregs.h.al;
  221.   
  222.   inregs.h.al = (verify_p) ? 1 : 0;
  223.   inregs.h.ah = 0x2E;
  224.   intdos(&inregs, &outregs);
  225.   
  226.   return old_flag;
  227. }
  228.  
  229. int dos_set_ctrl_c_check_flag(int check_p)
  230. { union REGS inregs, outregs;
  231.   int old_flag;
  232.   
  233.   inregs.x.ax = 0x3300;
  234.   intdos(&inregs, &outregs);
  235.   old_flag = outregs.h.dl;
  236.   
  237.   inregs.h.dl = (check_p) ? 1 : 0;
  238.   inregs.x.ax = 0x3301;
  239.   intdos(&inregs, &outregs);
  240.   
  241.   return old_flag;
  242. }
  243.  
  244. int dos_rename_file(const char *old, const char *new)
  245. {
  246.   union REGS inregs, outregs;
  247.   struct SREGS segregs;
  248.   
  249.   inregs.e.edx = (unsigned long) old;
  250.   inregs.e.edi = (unsigned long) new;
  251.   segread(&segregs);
  252.   segregs.es = segregs.ds;
  253.  
  254.   inregs.h.ah = 0x56;
  255.   intdosx(&inregs, &outregs, &segregs);
  256.   
  257.   if (outregs.x.cflag)
  258.     return DOS_FAILURE;
  259.   else
  260.     return DOS_SUCCESS;
  261. }
  262.  
  263. int dos_get_machine_name(char *name)
  264. {
  265.   union REGS inregs, outregs;
  266.   struct SREGS segregs;
  267.   
  268.   inregs.e.edx = (unsigned long) name;
  269.   segregs.ds = getDS();
  270.  
  271.   inregs.x.ax = 0x5E00;
  272.   intdosx(&inregs, &outregs, &segregs);
  273.   
  274.   if ((outregs.x.cflag) || (outregs.h.ch == 0))
  275.     return DOS_FAILURE;
  276.   else
  277.     return outregs.h.cl;
  278. }
  279.  
  280. int dos_drive_letter_to_number(char letter)
  281. {
  282.   if (letter == '\0')
  283.     return 0;
  284.   else if ((letter >= 'a')&&(letter <= 'z'))
  285.     return ((letter - 'a') + 1);
  286.   else if ((letter >= 'A')&&(letter <= 'Z'))
  287.     return ((letter - 'A') + 1);
  288.   else
  289.     return -1;
  290. }
  291.  
  292. char dos_drive_number_to_letter(int number)
  293. {
  294.   if ((number >= 1)&&(number <= 26))
  295.     return ('A' + (number - 1));
  296.   else
  297.     return '\0';
  298. }
  299.  
  300. int dos_set_default_drive(int drive_number)
  301. {
  302.   union REGS inregs, outregs;
  303.   
  304.   if (drive_number > 0)
  305.   {    
  306.     inregs.h.dl = drive_number - 1;
  307.     inregs.h.ah = 0x0E;
  308.     intdos(&inregs, &outregs);
  309.   }
  310.   return DOS_SUCCESS;
  311. }
  312.     
  313. int dos_get_default_drive(int drive_number)
  314. {
  315.   union REGS inregs, outregs;
  316.   
  317.   inregs.h.ah = 0x19;
  318.   intdos(&inregs, &outregs);
  319.   
  320.   return outregs.h.al + 1;
  321. }
  322.  
  323.  
  324. dos_boolean 
  325. dos_pathname_as_filename(char * name, char * buffer)
  326. { /* Returns whether directory encountered is top level */
  327.   unsigned int end_index = strlen(name) - 1;
  328.  
  329.   /* The runtime system comes down with a name that has a back slash
  330.      at the end.  This will choke DOS.
  331.    */
  332.   strcpy(buffer, name);
  333.   if ((end_index >= 0) && (buffer[end_index] == '\\'))
  334.   { /* Name is indeed a directory */
  335.     if (end_index == 0) /* if only one char, name is top */
  336.       return dos_true;
  337.     else
  338.     { if (buffer[end_index-1] == ':') /* Preceded by drive letter, top */
  339.       { return dos_true; }
  340.       else
  341.       { buffer[end_index] = '\0';
  342.     return dos_false;
  343.       }
  344.     }
  345.   }
  346.   else
  347.   { return dos_false; }
  348. }
  349.  
  350. int dos_split_filename(char * name, char * device, char * filename)
  351.   unsigned start;
  352.   int drive_number;
  353.   
  354.   if ((strlen(name) >= 2) && (name[1] == ':'))
  355.   { device[0] = name[0], device[1] = name[1], device[2] = '\0';
  356.     drive_number = dos_drive_letter_to_number(name[0]);
  357.     start = 2;
  358.   }
  359.   else
  360.   { device[0] = '\0';
  361.     drive_number = 0;
  362.     start = 0;
  363.   }
  364.   dos_pathname_as_filename(&name[start], filename);
  365.   return drive_number;
  366. }
  367.